<!DOCTYPE html>
<html lang="en">

<head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
</head>

<body>
      <script>
            // document.cookie
            // setCookie(); 增改一体;
            // getCookie(); 获取;
            // removeCookie(); 删除;

            // path 
            // domain 

            function setCookie(name, value, expires) {
                  // 核心 : 操作字符串 !;
                  var cookie_str = name + "=" + value;
                  //判定是否有必要增加过期时间;
                  if (typeof expires === "number") {
                        var d = new Date();
                        d.setDate(d.getDate() + expires);
                        cookie_str += ";expires=" + d;
                        console.log(cookie_str)
                  }
                  // 把拼接好的字符串设置到 document.cookie 上;
                  // console.log(cookie_str);
                  document.cookie = cookie_str;
            }

            // setCookie("asd", "13213", 3)
            // setCookie("asdsa", "1151")
            // 获取cookie;
            function getCookie(name) {
                  // 分割;
                  // 一定要以分号加空格的间隔进行分割;
                  var cookie_arr = document.cookie.split("; ");
                  for (var i = 0; i < cookie_arr.length; i++) {
                        // 每一条cookie => key=value;
                        var cookie_item = cookie_arr[i]
                        // console.log(cookie_item);
                        cookie_item = cookie_item.split("=");
                        if (cookie_item[0] == name) {
                              return cookie_item[1];
                        }
                  }
                  // return "";
            }
            // 删除 cookie ;
            function removeCookie(name) {
                  setCookie(name, "", -1);
            }


      </script>
</body>

</html>